package cn.fxl.user.service.impl;

import cn.fxl.basic.exception.BusinessExciption;
import cn.fxl.basic.service.impl.BasicServiceImpl;
import cn.fxl.basic.utils.Md5Utils;
import cn.fxl.basic.utils.StrUtils;
import cn.fxl.user.domain.Logininfo;
import cn.fxl.user.domain.User;
import cn.fxl.user.dto.UserDto;
import cn.fxl.user.mapper.LogininfoMapper;
import cn.fxl.user.mapper.UserMapper;
import cn.fxl.user.service.IUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

@Service
public class UserServiceImpl extends BasicServiceImpl<User> implements IUserService {
    @Autowired
    private JavaMailSender javaMailSender;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void remove(Long id) {
        User user = userMapper.getOne(id);
        logininfoMapper.remove(user.getLogininfo_id());
        super.remove(id);
    }

    @Override
    public void update(User user) {
        String password = user.getPassword();
        String complexRandomString = StrUtils.getComplexRandomString(32);
        String md5pad = Md5Utils.encrypByMd5(complexRandomString+password);
        user.setSalt(complexRandomString);
        user.setPassword(md5pad);
        User ogUser = userMapper.getOne(user.getId());
        Logininfo ogLogininfo = logininfoMapper.getOne(ogUser.getLogininfo_id());
        BeanUtils.copyProperties(user,ogLogininfo);
        ogLogininfo.setId(ogUser.getLogininfo_id());
        logininfoMapper.update(ogLogininfo);
        super.update(user);
    }

    /**
     * 邮箱注册1发送邮箱改变状态为禁用
     * @param user
     * @throws MessagingException
     */
    @Override
    public void save(User user) throws MessagingException {
        String email = user.getEmail();
        String password = user.getPassword();
        if(StringUtils.isEmpty(email)){
            throw new BusinessExciption("邮箱不能为空");
        }
        User userEmail=userMapper.getOneByEmail(email);
        if(userEmail!=null){
            throw new BusinessExciption("该邮箱已经注册");
        }
        String complexRandomString = StrUtils.getComplexRandomString(32);
        user.setSalt(complexRandomString);
        String md5Psd = Md5Utils.encrypByMd5(complexRandomString + password);
        Logininfo logininfo = new Logininfo();
        logininfoMapper.save(logininfo);
        user.setLogininfo_id(logininfo.getId());
        user.setPassword(md5Psd);
        user.setUsername(email);
        user.setState(0);
        userMapper.save(user);
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
        helper.setFrom("807018277@qq.com");
        helper.setSubject("用户注册激活邮件");
        helper.setText("<h1>注册成功<a href='http://localhost:8080/user/active/email/"+user.getId()+"'>点击激活</a></h1>",true);
//        helper.addAttachment("CgAIC2LMDkWAFbwbAAEHcv-x8dg205.jpg",new File("http://123.207.27.208/group1/M00/00/73/CgAIC2LMDkWAFbwbAAEHcv-x8dg205.jpg"));
        helper.setTo(user.getEmail());
        javaMailSender.send(mimeMessage);
    }
    /**
     *
     * 用户邮箱注册激活
     * @param id
     */
    @Override
    public void emailActive(Long id) {
        User user = userMapper.getOne(id);
        user.setState(1);
        Logininfo logininfo=user2Logininfo(user);
        logininfo.setId(user.getLogininfo_id());
        logininfoMapper.update(logininfo);
        userMapper.update(user);
    }

    /**
     * 手机注册
     * @param userDto
     */
    @Override
    public void phoneRegister(UserDto userDto) {
        String password = userDto.getPassword();
        String passwordRepeat = userDto.getPasswordRepeat();
        String phone = userDto.getPhone();
        String phoneCode = userDto.getPhoneCode();
        if(StringUtils.isEmpty(password) || StringUtils.isEmpty(passwordRepeat) || StringUtils.isEmpty(phone) ||StringUtils.isEmpty(phoneCode)){
            throw new BusinessExciption("有信息未填");
        }
        Object obj = redisTemplate.opsForValue().get("register:" + phone);
        if(obj==null){
            throw new BusinessExciption("手机验证码过期");
        }
        if(obj.toString().equals(phoneCode)){
            throw new BusinessExciption("手机验证码输入错误");
        }
        User user=userDto2User(userDto);
        Logininfo logininfo=user2Logininfo(user);
        logininfoMapper.save(logininfo);
        user.setLogininfo_id(logininfo.getId());
        userMapper.save(user);
    }


    private User userDto2User(UserDto userDto) {
        User user = new User();
        user.setUsername(userDto.getPhone());
        user.setPhone(userDto.getPhone());
        user.setState(1);
        String complexRandomString = StrUtils.getComplexRandomString(32);
        user.setSalt(complexRandomString);
        String md5Password = Md5Utils.encrypByMd5(complexRandomString+userDto.getPassword());
        user.setPassword(md5Password);
        return user;
    }
    private Logininfo user2Logininfo(User user) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user,logininfo);
        logininfo.setType(1);
        return logininfo;
    }
}
